package top.misspro.valheimadminsocket.event;

import lombok.extern.slf4j.Slf4j;
import top.misspro.valheimadminsocket.message.Action;
import top.misspro.valheimadminsocket.message.command.ServerInfo;
import top.misspro.valheimadminsocket.server.Client;
import top.misspro.valheimadminsocket.server.ServerContains;
import top.misspro.valheimadminsocket.util.ChannelUtils;
import top.misspro.valheimadminsocket.util.JSON;

import java.io.IOException;

/**
 * @author liulinbo
 */
@Slf4j
public class ConnectEventListener implements ServerEventListener {
    @Override
    public void handle(Client client, String data) {
        log.info("客户端服务器连接 {}", data);
        try {
            ServerInfo serverInfo = JSON.parserObject(data, ServerInfo.class);
            log.info("客户端配置 {}", serverInfo);
            ChannelUtils.sendToClient(client.getChannel(), Action.Client.OK, null, "连接成功");
            client.setStatus(ServerContains.ServerStatus.CONNECTED);
        } catch (IOException e) {
            log.error("消息解码失败");
            ChannelUtils.sendToClient(client.getChannel(), Action.Client.FAILED, null, "消息解码失败");
        }
    }
}
